package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.MessageDao;
import com.suyang.syt.dao.interfaces.UserMessageDao;
import com.suyang.syt.model.Message;
import com.suyang.syt.model.UserMessage;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.MessageDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.service.interfaces.UserMessageService;
import com.suyang.syt.util.EmptyUtil;

@Service
public class UserMessageServiceImpl implements UserMessageService {

	@Autowired
	private UserMessageDao userMessageDao;
	@Autowired
	private MessageDao messageDao;

	@Override
	public PagerEasyUI<MessageDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, UserMessage userMessage) throws Exception {
		StringBuilder hql = new StringBuilder("from UserMessage u where 1=1 ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and u.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and u.createTime <= '").append(createDate2).append("' ");
		}
		if (userMessage.getUserId() > 0) {
			hql.append(" and u.userId= ").append(userMessage.getUserId());
		}
		if (EmptyUtil.isNotEmpty(userMessage.getMessageName())) {
			hql.append(" and u.messageName like'%").append(userMessage.getMessageName()).append("%'");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by u.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<MessageDTO> easyUIPager = new PagerEasyUI<MessageDTO>();
		List<UserMessage> userMessages = userMessageDao.findByEasyUI(hql.toString(), page, rows);
		String ids = "";
		for (UserMessage userMessage2 : userMessages) {
			ids += userMessage2.getMessageId() + ",";
		}
		List<Message> messages = new ArrayList<Message>();
		if (EmptyUtil.isNotEmpty(ids)) {
			ids = ids.substring(0, ids.length() - 1);
			messages = messageDao.list("from Message where status=1 and id in(" + ids + ")");
		}
		List<MessageDTO> messageDTOs = new ArrayList<MessageDTO>();
		for (Message message : messages) {
			MessageDTO messageDTO = new MessageDTO(message);
			messageDTOs.add(messageDTO);
		}
		easyUIPager.setRows(messageDTOs);
		easyUIPager.setTotal((Long) messageDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj delete(String ids, long userId) throws Exception {
		if (EmptyUtil.isEmpty(ids)) {
			return new AjaxObj(0, "id参数错误");
		}
		userMessageDao.deleteByMessageIds(ids, userId);
		return new AjaxObj(1, "删除成功");
	}

	/** 根据用户uid查询用户的消息 ***/
	@Override
	public PagerEasyUI<MessageDTO> listByEasyUI(int page, int rows, String sort, String order, String createDate1, String createDate2, long uid, String status) throws Exception {
		StringBuilder hql = new StringBuilder("from UserMessage u where 1=1 ");
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and u.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and u.createTime <= '").append(createDate2).append("' ");
		}
		if (uid > 0) {
			hql.append(" and u.userId= ").append(uid);
		}
		if (EmptyUtil.isNotEmpty(status)) {
			hql.append(" and u.status= ").append(Integer.parseInt(status));
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by u.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<MessageDTO> easyUIPager = new PagerEasyUI<MessageDTO>();
		List<UserMessage> userMessages = userMessageDao.findByEasyUI(hql.toString(), page, rows);
		String ids = "";
		for (UserMessage userMessage2 : userMessages) {
			ids += userMessage2.getMessageId() + ",";
		}
		List<Message> messages = new ArrayList<Message>();
		if (EmptyUtil.isNotEmpty(ids)) {
			ids = ids.substring(0, ids.length() - 1);
			messages = messageDao.list("from Message where status=1 and id in(" + ids + ")");
		}
		List<MessageDTO> messageDTOs = new ArrayList<MessageDTO>();
		for (Message message : messages) {
			MessageDTO messageDTO = new MessageDTO(message);
			for (UserMessage um : userMessages) {
				if (um.getMessageId() == messageDTO.getId()) {
					messageDTO.setCheckStatus(um.getStatus());
				}
			}
			messageDTOs.add(messageDTO);
		}
		easyUIPager.setRows(messageDTOs);
		easyUIPager.setTotal((Long) messageDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj update(long uid, String ids) throws Exception {
		if (EmptyUtil.isEmpty(ids)) {
			return new AjaxObj(0, "id参数错误");
		}
		userMessageDao.update(uid, ids);
		return new AjaxObj(1, "消息已读！");
	}

	@Override
	public List<UserMessage> list(String ids, long userId) throws Exception {
		String hql = " from UserMessage u where u.userId=" + userId + " and  u.id in(" + ids + ")";
		return userMessageDao.list(hql);
	}

}
